# encoding:utf-8
import os
import time
from PIL import Image

path = "F:\\data\\Porn\\A4U003\\images"
Image.LOAD_TRUNCATED_IMAGES = True


def get_image_size(file):
    im = Image.open(file)  # 返回一个Image对象
    return im.size[0], im.size[1]


def create_thumbnail(full_file_path):
    im = Image.open(full_file_path)
    # If there 's "image file is truncated" ERROR, a debug method can help to find images which lacks EOF.
    # print(full_file_path)
    im.thumbnail((105, 69))
    file_name = full_file_path.split("\\")[-1]
    sub_dir_name = full_file_path.split("\\")[-2]
    thumbnail_name = path + "\\gallery\\" + sub_dir_name + file_name
    im.save(thumbnail_name, 'JPEG')
    return thumbnail_name.split("\\")[-1]


def get_files(path):
    counter = 0
    content = "var _listdata= [];\n"
    thumbnail_names = []
    for root, dirs, files in os.walk(path):
        # for root, subdirs, files in os.walk(root):
        imagebase = root.split("\\")[-2]
        prefix = root.split("\\")[-1]
        # print(prefix)
        if root.find("\gallery") > 0:
            continue

        for file in files:
            if root == path and file.find(".db") > 0:
                continue
            full_file_name = root + "\\" + file
            img_size = get_image_size(full_file_name)
            big_image_name = "./" + imagebase + "/" + prefix + "/" + file
            thumbnail_name = imagebase + "/gallery/" + create_thumbnail(full_file_name)
            thumbnail_names.append(thumbnail_name)
            content = content + "_listdata[" + str(counter) + "] = {\n"
            content = content + "\ttitle:\'" + big_image_name + "\',\n"
            content = content + "\ttimg:\'" + big_image_name + "\',\n"
            content = content + "\timg:\'" + thumbnail_name + "\',\n"
            content = content + "\tlistimg:\'" + thumbnail_name + "\',\n"
            content = content + "\tpicwidth:\'" + str(img_size[0]) + "\',\n"
            content = content + "\tpicheight:\'" + str(img_size[1]) + "\',\n"
            content = content + "\tmorelink:\'#\'\n"
            content = content + "};\n"
            counter = counter + 1
    content = content + "new ifeng.Gallery({\n"
    content = content + "	photoViewMode:\"skip\"//\"loop\":循环/ \"skip\":下一图集\n"
    content = content + "	,activeThumbCls:\"current\"//预览图选中的类名\n"
    content = content + "	,data:_listdata\n"
    content = content + "}); \n"
    # print(path +"\n" + path[:-7])
    output_file = open(path[:-7] + "\\js\\piclist.js", "w", encoding="utf-8")
    output_file.write(content)
    output_file.close()
    print(content)
    print(thumbnail_names)
    output_index_file_r = open(path[:-7] + "\\index.html", "r", encoding="utf-8")
    content = output_index_file_r.read()
    pos1 = content.find("<!--number-->")
    pos2 = content.find("<!--date-->")
    pos3 = content.find("<!-- thumb_list -->")
    content1 = content[:pos1]
    content_suffix1 = content[pos1:pos2].replace("<!--number-->", "")
    content_suffix2 = content[pos2:pos3].replace("<!--date-->", "")
    content_suffix3 = content[pos3:].replace("<!-- thumb_list -->", "")
    # print(content_suffix3)
    content1 = content1 + str(len(thumbnail_names))
    content1 = content1 + content_suffix1
    content1 = content1 + time.strftime("%Y/%m/%d")
    content1 = content1 + content_suffix2
    content1 = content1 + "<li><a href=\"#p=1\" hidefocus=\"true\"><img src=\"./" + thumbnail_names[0] + "\"/></a></li>\n"
    content1 = content1 + "<li><a href=\"#p=2\" hidefocus=\"true\"><img src=\"./" + thumbnail_names[1] + "\"/></a></li>\n"
    content1 = content1 + "<li><a href=\"#p=3\" hidefocus=\"true\"><img src=\"./" + thumbnail_names[2] + "\"/></a></li>\n"
    content1 = content1 + "<li><a href=\"#p=4\" hidefocus=\"true\"><img src=\"./" + thumbnail_names[3] + "\"/></a></li>\n"
    content1 = content1 + "<li><a href=\"#p=5\" hidefocus=\"true\"><img src=\"./" + thumbnail_names[4] + "\"/></a></li>\n"
    content1 = content1 + content_suffix3
    # print(content[:pos])
    # print("------------------------\n")
    # print(content[pos:].replace("<!-- thumb_list -->", ""))

    # print(content[pos:])<a href="#p=1" hidefocus="true"><img src="./images/gallery/Serialjudy01a001.jpg"></a>
    # if pos != -1:
    #     content = content[:pos]
    #

    #     content = content + content_suffix
    output_index_file_w = output_index_file_r = open(path[:-7] + "\\index.html", "w", encoding="utf-8")
    output_index_file_w.write(content1)
    output_index_file_r.close()
    output_index_file_w.close()

if __name__ == "__main__":
    get_files(path)
    # print(get_image_size("E:\\data\\Street_Galleries\\001\\images\\gallery\\xes_6ba4c649e427d105bf8296db9906ced7.jpg"))
    # create_thumbnail("E:\\data\\Street_Galleries\\001\\images\\2009-07-20成都的两个时尚美女\\001.jpg")